package com.lexicalAnalyzer;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * User: Lenovo
 * Date: 23.11.2012
 * Time: 12:00
 * To change this template use File | Settings | File Templates.
 */
public class Scanner extends ScannerBase
{
    FlexLexer lexer;
    public SymbolTable symbolTable;

    // smiður
    public Scanner(InputStream inputStream, SymbolTable symTab)
    {
        lexer = new FlexLexer(inputStream);
        this.symbolTable = symTab;
    }



    @Override
    protected void setCurrentToken(Token token) {
        m_currentToken = token;
    }

    @Override
    protected TokenCode getKeywordCode(String lexeme)
    {
        return getKeywordMap().get(lexeme);
    }

    protected Token getCurrentToken()
    {
        return this.m_currentToken;
    }



   /* It is optional if you let the scanner recognize the keywords or not. If you choose not
    to (more lightweight lex.yy.cc) then if tCode == tc ID you need to check if the current
    token is a keyword. If that is the case, then you should change tCode appropriately, as
    well as setting Type = dt KEYWORD. Each keyword has a separate token name.
        Once this is done you need to call the function setCurrentToken() to set the
    attribute elds of the current token (SymbolTableEntry* excluded; see below) and return
    a pointer to the token at the end (note that nextToken() can access the global variables
    Oper og Type).
    */

    @Override
    public Token nextToken()    throws IOException
    {
        Token token = lexer.yylex();

        while(token.getTokenCode() == TokenCode.COMMENT || token.getTokenCode() == TokenCode.NEWLINE || token.getTokenCode() == TokenCode.SPACE || token.getTokenCode() == TokenCode.TAB)
        {
            while(token.getTokenCode() == TokenCode.COMMENT)
            {
                token = lexer.yylex();
            }
            while(token.getTokenCode() == TokenCode.NEWLINE)
            {
                token = lexer.yylex();
            }
            while(token.getTokenCode() == TokenCode.SPACE)
            {
                token = lexer.yylex();
            }
            while(token.getTokenCode() == TokenCode.TAB)
            {
                token = lexer.yylex();
            }
        }

        if(token.getTokenCode() == TokenCode.ID || token.getTokenCode() == TokenCode.NUMBER)
        {
            this.symbolTable.Insert(token.getDataValue().toString());
        }


        System.out.println("LastToken: " + token.getTokenCode());
        setCurrentToken(token);
        return token;
    }




}
